package form.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormatAlign;
	
	import form.events.AlertEvent;
	import form.ui.component.Alert.AlertBackground;
	import form.ui.core.Button;

	public class Alert extends Sprite
	{
		public static const FONT_COLOR:uint = 0x0;
		public static const BUTTON_COLOR:uint = 0x333333;
		public static const MODEL_COLOR:uint = 0x000000;
		public static const MODEL_ALPHA:Number = 0.4;
		
		public static const DISPLAY_OK:uint = 0;
		public static const DISPLAY_ALL:uint = 1;
		public static const DISPLAY_CANCEL:uint = 2;
		
		public static const DEFAULT_WIDTH:uint = 260;
		public static const DEFAULT_HEIGHT:uint = 130;	
		
		public static var modelLayout:Sprite;		
						
		public var padding:uint = 10;
		public var horCenter:Boolean = true;
		public var verCenter:Boolean = true;
		public var minButtonWidth:uint = 60;
		public var minButtonHeight:uint = 25;
		public var titlePadding:uint = 5;
		public var buttonGap:uint = 10;
			
		private var okButton:FormButton;
		private var cancelButton:FormButton;
		
		protected var titleLabel:Label;
		protected var messageText:Text;
		
		protected var titleBar:Sprite;
		
		public function Alert()
		{
			super();
			
			okButton = new FormButton();
			cancelButton = new FormButton();
			
			this.backSkin = new AlertBackground();
			this.titleBar = new Sprite();
						
			titleLabel = new Label();
			titleLabel.x = titlePadding;
			titleLabel.y = titlePadding;
			titleLabel.fontColor = FONT_COLOR;
			addChild(titleLabel);
			
			messageText = new Text();
			messageText.x = padding;
			messageText.fontColor = FONT_COLOR;
			messageText.textAlign = TextFormatAlign.CENTER;
			addChild(messageText);
			
			okButton.height = minButtonHeight;
			okButton.width = minButtonWidth;
			okButton.labelColor = BUTTON_COLOR;
			okButton.label = _okLable;
			okButton.addEventListener(MouseEvent.CLICK, onCloseAlert, false, 0, true);
						
			cancelButton.height = minButtonHeight;
			cancelButton.width = minButtonWidth;
			cancelButton.labelColor = BUTTON_COLOR;
			cancelButton.label = _cancelLable;
			cancelButton.addEventListener(MouseEvent.CLICK, onCloseAlert, false, 0, true);
			
			titleBar.buttonMode = true;
			drawTitleBar();
			addChild(titleBar);
			
			titleBar.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag, false, 0, true);
			titleBar.addEventListener(MouseEvent.MOUSE_UP, onStopDrag, false, 0, true);			
		}
		
		public static function show(message:String = "", title:String = "", mode:uint = 0, model:Boolean = true):Alert
		{
			var application:ApplicationContainer = ApplicationContainer.application;
			var sw:Number = application.stage.stageWidth;
			var sh:Number = application.stage.stageHeight;
			
			var alert:Alert = new Alert();
			alert.titleLabel.formValue = title;
			alert.messageText.formValue = message;
			alert.messageText.width = alert.width - alert.padding * 2;
			alert.messageText.y = (alert.height - alert.minButtonHeight) / 2
			
			alert.okButton.y = alert.height - alert.okButton.height - 5;
			alert.cancelButton.y = alert.okButton.y;
			
			switch(mode)
			{
				case DISPLAY_OK:
					alert.okButton.x = alert.width / 2 - alert.okButton.width/2;
					alert.addChild(alert.okButton);
					break;
				case DISPLAY_CANCEL:
					alert.cancelButton.x = alert.width / 2 - alert.cancelButton.width/2;
					alert.addChild(alert.cancelButton);
					break;
				case DISPLAY_ALL:
					alert.okButton.x = alert.width / 2 - alert.okButton.width - alert.buttonGap;
					alert.addChild(alert.okButton);
					alert.cancelButton.x = alert.buttonGap + alert.width / 2;
					alert.addChild(alert.cancelButton);
					break;
			}
			
			resetModelLayout(model, sw, sh);
			
			alert.x = sw/2- alert.width / 2;
			alert.y = sh/2 - alert.height / 2;
			application.addOverlay(alert);
			return alert;
		}
		
		private static function resetModelLayout(display:Boolean, width:Number, height:Number):void
		{
			var application:ApplicationContainer = ApplicationContainer.application;
			if(modelLayout)
			{
				application.removeOverlay(modelLayout);
			}
			
			if(display)
			{
				if(!modelLayout) modelLayout = new Sprite();
				modelLayout.graphics.clear();
				modelLayout.graphics.beginFill(MODEL_COLOR, MODEL_ALPHA);
				modelLayout.graphics.drawRect(0,0,width, height);
				modelLayout.graphics.endFill();
				
				application.addOverlay(modelLayout);
			}
			
		}
		
		private var _okLable:String = "确定";
		public function set okLabel(value:String):void
		{
			_okLable = value;
			okButton.label = _okLable;
		}
		public function get okLabel():String
		{
			return _okLable;
		}
		
		private var _cancelLable:String = "取消";
		public function set cancelLable(value:String):void
		{
			_cancelLable = value;
			cancelButton.label = _cancelLable;
			
		}
		public function get cancelLable():String
		{
			return _cancelLable;
		}
		
		
		private var _backSkin:DisplayObject;
		public function set backSkin(value:DisplayObject):void
		{
			if(_backSkin && contains(_backSkin))removeChild(_backSkin);
			_backSkin = addChild(value);
		}
		public function get backSkin():DisplayObject
		{
			return _backSkin;
		}
		
		protected function drawTitleBar():void
		{
			titleBar.graphics.clear();
			titleBar.graphics.beginFill(0,0);
			titleBar.graphics.drawRect(0,0,this.width, this.titleLabel.height);
			titleBar.graphics.endFill();
		}
		
		override public function get width():Number
		{
			return backSkin?backSkin.width : super.width;
		}
		
		override public function get height():Number
		{
			return backSkin?backSkin.height : super.height;
		}
		
		private function onStartDrag(e:MouseEvent):void
		{			
			this.startDrag();
		}
		
		private function onStopDrag(e:MouseEvent):void
		{
			this.stopDrag();
		}
		
		private function onCloseAlert(e:Event):void
		{
			var application:ApplicationContainer = ApplicationContainer.application;
			var button:Button = e.currentTarget as Button;
			if(button)
			{
				var alert:Alert = button.parent as Alert;
				if(alert)
				{
					application.removeOverlay(alert);
				}
			}
			var evt:AlertEvent;
			if(e.target == okButton)
			{
				evt = new AlertEvent(AlertEvent.ALERT_RESULT, true);
			}
			else if(e.target == cancelButton)
			{
				evt = new AlertEvent(AlertEvent.ALERT_RESULT, false);
			}
			
			if(modelLayout)
			{
				application.removeOverlay(modelLayout);
			}
			
			dispatchEvent(evt);
		}
	}
}
